package com.javasm.adminapi.module.system.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.javasm.adminapi.common.R;
import com.javasm.adminapi.module.system.dto.IdsDTO;
import com.javasm.adminapi.module.system.dto.PerDTO;
import com.javasm.adminapi.module.system.dto.PermissionQueryDTO;
import com.javasm.adminapi.module.system.entity.Permission;
import com.javasm.adminapi.module.system.service.PermissionService;
import com.javasm.adminapi.module.system.service.RolePermissionService;
import com.javasm.adminapi.module.system.service.impl.RolePermissionServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 权限表(Permission)表控制层
 *
 * @author makejava
 * @since 2025-10-18 15:21:30
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {
    @Resource
    PermissionService permissionService;
    @Autowired
    private RolePermissionService rolePermissionService;

    @GetMapping("/page")
    public R page(PermissionQueryDTO permissionQueryDTO){
        PageInfo<Permission> list = permissionService.queryPage(permissionQueryDTO);
        return R.ok(list);
    }
    @DeleteMapping("/del")
    public R del( String ids){
        List<Long> collect = Arrays.stream(ids.split(","))
                .map(Long::parseLong).collect(Collectors.toList());
        permissionService.del(collect);
        return R.ok();
    }

    @PostMapping("/add")
    public R add(@RequestBody Permission permission){
        permissionService.add(permission);
        return R.ok();
    }

    @PutMapping("/update")
    public R update(@RequestBody Permission permission){
        permissionService.updatePermission(permission);
        return R.ok();
    }

    @PostMapping("/updateStatus")
    public R updateStatus(@RequestBody Permission permission){
        permissionService.updateStart(permission);
        return R.ok();
    }


    @GetMapping("/list")
    public R list(){
        List<Permission> getlist = permissionService.getlist();
        return R.ok(getlist);
    }

    @GetMapping("/listByRid")
    public R listByRid (Integer id){
        List<Permission> getByRid = permissionService.getByRid(Long.valueOf(id));
        return R.ok(getByRid);
    }

    @PostMapping("/savesys")
    public R savesys(@RequestBody PerDTO permission){
        rolePermissionService.savePerDTO(permission);
        return R.ok();
    }
}

